package com.yinNuo.collectWool.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yinNuo.collectWool.core.Response;
import com.yinNuo.collectWool.core.constant.Costant;
import com.yinNuo.collectWool.mobileApi.entity.SysUser;
import com.yinNuo.collectWool.mobileApi.service.SysUserService;
import com.yinNuo.collectWool.utils.JwtTokenUtil;
import com.yinNuo.collectWool.utils.ToolUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author: yinNuo
 * create at: 2020/5/15 18:20
 * @description: 访问首页
 */
@Controller
public class IndexController {

    @Resource
    private SysUserService sysUserService;

    //首页
    @RequestMapping("/")
    public String index() {
        return "index";
    }

    /**
     * 注册功能
     * @return
     */
    @RequestMapping("/resign")
    @ResponseBody
    public Response resign(@RequestBody SysUser sysUser, HttpServletRequest request){
        //System.out.println(request);
        Enumeration<String> cookie = request.getHeaders("Cookie");
        if (ToolUtil.isEmpty(sysUser.getUserName()) &&ToolUtil.isEmpty(sysUser.getPassword())) {
            return Response.failed(Costant.SIGN_ERROR);
        }
        sysUser.setCreateTime(new Date());
        sysUser.setIsDel(0);
        sysUserService.save(sysUser);
        return Response.ok(Costant.SIGN_SUCCESS);
    }

    /**
     * 系统登录功能
     * @return
     */
    @RequestMapping("/loginIn")
    @ResponseBody
    public Response login(@RequestBody SysUser sysUser, HttpServletResponse reponse) {
        if (ToolUtil.isEmpty(sysUser.getUserName()) &&ToolUtil.isEmpty(sysUser.getPassword())) {
            return Response.failed(Costant.SIGN_ERROR);
        }
        List<SysUser> list = sysUserService.list(new QueryWrapper<SysUser>().eq("user_name", sysUser.getUserName()).eq("password", sysUser.getPassword()).eq("is_del",0));
        if (!list.isEmpty()) {
            //根据用户id生成token
            SysUser getSysUser = list.get(0);
            String userId = getSysUser.getId().toString();
            String token = JwtTokenUtil.generateToken(userId);
            reponse.setHeader("token",token);
            return Response.ok(getSysUser);
        }
        return Response.failed(Costant.LOGIN_FAILED);
    }

    /**
     * 忘记密码，根据手机号找回 恢复默认密码 111111
     * @return
     */
    @RequestMapping("/forget")
    @ResponseBody
    public Response forget(@RequestBody SysUser sysUser){
        if (ToolUtil.isEmpty(sysUser.getPhone())) {
            return Response.ok(Costant.FIND_PASSWORD);
        }
        List<SysUser> list = sysUserService.list(new QueryWrapper<SysUser>().eq("phone", sysUser.getPhone()));
        if (!list.isEmpty()) {
            boolean isSuccess = sysUserService.update(new UpdateWrapper<SysUser>().set("password", "111111").eq("id",list.get(0).getId()));
            Map<String, Object> map = new HashMap<>();
            if (isSuccess) {
                map.put("msg", Costant.NEW_PASSWORD);
                map.put("user", list.get(0));
                return Response.ok(map);
            }
        }
        return Response.failed(Costant.FIND_RESULT);
    }

    /**
     * 更新用户信息
     * @return
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public Response updateUser(@RequestBody SysUser sysUser){
        if (ToolUtil.isEmpty(sysUser.getId()) ) {
            return Response.failed(Costant.UPDATE_USER);
        }
        boolean update = sysUserService.update(sysUser, new UpdateWrapper<SysUser>());
        if (update) {
            return Response.ok(Costant.UPDATE_USER_SUCCESS);
        }
        return Response.failed(Costant.UPDATE_USER_FAILED);
    }

    /**
     * 删除用户
     * @return
     */
    @RequestMapping("/deleteUser")
    @ResponseBody
    public Response deleteUser(@RequestBody SysUser sysUser){
        if (ToolUtil.isEmpty(sysUser.getId()) ) {
            return Response.failed(Costant.UPDATE_USER);
        }
        boolean b = sysUserService.removeById(sysUser);
        if (b) {
            return Response.ok(Costant.DELETE_USER_SUCCESS);
        }
        return Response.failed(Costant.DELETE_USER_FAILED);
    }

}
